/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "makecard_srv_stub.h"
#include <string.h>
#include "message_option.h"
#include "message_parcel.h"
#include "hilog_wrapper.h"
#include "makecard_common.h"
#include "makecard_errors.h"

namespace SwanLink {
namespace Finance {
int MakecardSrvStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    MAKECARD_HILOGD(MAKECARD_SRV, "cmd = %{public}d, flags = %{public}d", code, option.GetFlags());
    std::u16string descriptor = MakecardSrvStub::GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    if (descriptor != remoteDescriptor) {
        MAKECARD_HILOGE(MAKECARD_SRV, "Descriptor is not matched");
        return ERR_NAME_NOT_FOUND;
    }
    return CheckRequestCode(code, data, reply, option);
}

int32_t MakecardSrvStub::CheckRequestCode(const uint32_t code, MessageParcel& data, MessageParcel& reply,
    MessageOption& option)
{
    switch (code) {
        case static_cast<int>(IMakecardSrv::MAKECARD_SETCONFIG): {
            return SetConfigStub(data, reply, option);
        }
        case static_cast<int>(IMakecardSrv::MAKECARD_SETSTANDBYPARAM): {
            return SetStandbyParamStub(data, reply, option);
        }
        case static_cast<int>(IMakecardSrv::MAKECARD_PRINTCARD): {
            return PrintCardStub(data, reply, option);
        }
        case static_cast<int>(IMakecardSrv::MAKECARD_GETDEVICEINFO): {
            return GetDeviceInfoStub(data, reply, option);
        }
        case static_cast<int>(IMakecardSrv::MAKECARD_GETERRORSTRING): {
            return GetErrorStringStub(data, reply, option);
        }
        case static_cast<int>(IMakecardSrv::MAKECARD_MOVECARD): {
            return MoveCardStub(data, reply, option);
        }
        case static_cast<int>(IMakecardSrv::MAKECARD_FLIPCARD): {
            return FlipCardStub(data, reply, option);
        }
        default:
        return CashDeviceSrvStub::OnRemoteRequest(code, data, reply, option);
    }
    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}


int32_t MakecardSrvStub::SetConfigStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;
    int configType = -1;
    int configValue = -1;

    READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);
    READ_PARCEL_WITH_RET(data, Int32, configType, E_READ_PARCEL_ERROR);
    READ_PARCEL_WITH_RET(data, Int32, configValue, E_READ_PARCEL_ERROR);

    int result = SetConfig(handle, configType, configValue);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, E_WRITE_PARCEL_ERROR);

    return ERR_OK;
}

int32_t MakecardSrvStub::SetStandbyParamStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;
    int dir = -1;
    int pos = -1;
    int standbyTime = -1;

    READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);
    READ_PARCEL_WITH_RET(data, Int32, dir, E_READ_PARCEL_ERROR);
    READ_PARCEL_WITH_RET(data, Int32, pos, E_READ_PARCEL_ERROR);
    READ_PARCEL_WITH_RET(data, Int32, standbyTime, E_READ_PARCEL_ERROR);

    int result = SetStandbyParam(handle, dir, pos, standbyTime);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, E_WRITE_PARCEL_ERROR);

    return ERR_OK;
}

int32_t MakecardSrvStub::PrintCardStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;
    BmpInfo *cardBmpInfo = nullptr;
    MAKECARD_HILOGE(MAKECARD_SRV,
        "MakecardSrvStub::PrintCardStub pre READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);)");
    READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);
    READ_BUFFER_WITH_RET(data, BmpInfo, cardBmpInfo, sizeof(cardBmpInfo), E_READ_PARCEL_ERROR);
    MAKECARD_HILOGE(MAKECARD_SRV,
        "MakecardSrvStub::PrintCardStub after READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);)");
    int result = PrintCard(handle, *cardBmpInfo);
    MAKECARD_HILOGE(MAKECARD_SRV, "MakecardSrvStub::PrintCardStub(%{public}d)", cardBmpInfo->direction);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, E_WRITE_PARCEL_ERROR);

    return ERR_OK;
}

int32_t MakecardSrvStub::GetDeviceInfoStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;
    int infoType = -1;
    char infoValue[128] = {0};

    READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);
    READ_PARCEL_WITH_RET(data, Int32, infoType, E_READ_PARCEL_ERROR);

    int result = GetDeviceInfo(handle, infoType, infoValue);
    WRITE_PARCEL_WITH_RET(reply, String, infoValue, E_WRITE_PARCEL_ERROR);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, E_WRITE_PARCEL_ERROR);

    return ERR_OK;
}

int32_t MakecardSrvStub::GetErrorStringStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;
    int status = -1;
    char statusDesc[128] = {0};

    READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);
    READ_PARCEL_WITH_RET(data, Int32, status, E_READ_PARCEL_ERROR);

    int result = GetErrorString(handle, status, statusDesc);
    WRITE_PARCEL_WITH_RET(reply, String, statusDesc, E_WRITE_PARCEL_ERROR);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, E_WRITE_PARCEL_ERROR);
    return ERR_OK;
}

int32_t MakecardSrvStub::MoveCardStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;
    int postionMoveTo = -1;

    READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);
    READ_PARCEL_WITH_RET(data, Int32, postionMoveTo, E_READ_PARCEL_ERROR);

    int result = MoveCard(handle, postionMoveTo);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, E_WRITE_PARCEL_ERROR);

    return ERR_OK;
}

int32_t MakecardSrvStub::FlipCardStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;

    READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);

    int result = FlipCard(handle);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, E_WRITE_PARCEL_ERROR);

    return ERR_OK;
}

}  // namespace Finance
}  // namespace SwanLink
